package com.allenkerr.queue1;

import java.util.function.Consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OrderIdConsumer extends Thread {

  private static final Logger logger = LoggerFactory.getLogger(OrderIdConsumer.class);

  private RedisTaskContainer container;
  private Consumer<Integer> consumer;

  public OrderIdConsumer(RedisTaskContainer container, Consumer<Integer> consumer) {
    this.container = container;
    this.consumer = consumer;
  }

  @Override
  public void run() {

    System.out.println("---------------------------------");
    System.out.println("OrderIdConsumer.run");
    System.out.println("---------------------------------\n");

    try {
      while (true) {
        //cast exception? you should check.
        Integer value = container.getRedisQueue1().takeFromTail();
        System.out.println("value = " + value);
        //逐个执行
        if (value != null) {
          try {
            consumer.accept(value);
          } catch (Exception e) {
            logger.error("调用失败", e);
          }
        }
      }
    } catch (Exception e) {
      logger.error("轮循线程异常退出", e);
    }
  }
}